/*
* $Id: ViewFieldValuesHelper.cpp 157 2010-09-29 19:15:28Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewFieldValuesHelper.h"
#include "Database/Tables/UserFieldsTable.h"
#include "Database/Tables/FieldValuesTable.h"
#include "Visual/Drawers/TableDrawer/TableDrawer.h"
#include "Visual/Drawers/TableDrawer/TableStructs.h"
#include "Visual/Drawers/EditorDrawer/EditorDrawer.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/SectionItem.h"
#include "../JournalDefined.h"

CViewFieldValuesHelper::CViewFieldValuesHelper(IDrawer* pDrawer){
	m_db = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	m_pDrawer = pDrawer;
}

CViewFieldValuesHelper::~CViewFieldValuesHelper(){
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(m_db);
}

void CViewFieldValuesHelper::GenerateUserHeaders(TypeField::table tp, int nBeginId){
	if(!m_pDrawer->IsValidDrawer(Drawer::table))
		return;
	CUserFieldsTable userFieldsTable;
	userFieldsTable.m_content.s_table = tp;
	IResult* pRes = userFieldsTable.GetValue(m_db);
	int idBegin = 0;
	for(size_t i = 0; i < pRes->GetSizeResult(); i++){
		CUserFieldsTable::content* pContent = (CUserFieldsTable::content*)((*pRes)[(int)i]);
		m_aUserHeaders.push_back(((CTableDrawer*)m_pDrawer)->AddHeader(pContent->s_strName,
			nBeginId + (int)i, pContent->s_type));
	}
	pRes->Free();
}

void CViewFieldValuesHelper::GenerateUserCels(CTableCelRow* pCelRow, int nIdRecord){
	if(!m_pDrawer->IsValidDrawer(Drawer::table))
		return;
	CFieldValuesTable fieldValuesTable;
	fieldValuesTable.m_content.s_nId = nIdRecord;
	IResult* pResult = fieldValuesTable.GetValue(m_db);
	for(std::vector<tableHeader*>::iterator it = m_aUserHeaders.begin(); it != m_aUserHeaders.end(); it++){
		std::tstring strValue;
		for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
			CFieldValuesTable::content* pContent = (CFieldValuesTable::content*)(*pResult)[i];
			if(pContent->s_strName == (*it)->m_strValue){
				strValue = pContent->s_strValue;
				break;
			}
		}
		pCelRow->AddCel(*it, strValue);
	}
	pResult->Free();
}

int CViewFieldValuesHelper::GenerateUserEditItems(TypeField::table tp, int nBeginId, int nIdRecord, std::map<int, std::tstring>& aCurValues){
	if(!m_pDrawer->IsValidDrawer(Drawer::editor))
		return 0;
	std::map<std::tstring, std::tstring> aMapValues;
	CFieldValuesTable fieldValues;
	fieldValues.m_content.s_nId = nIdRecord;
	IResult* pResult = fieldValues.GetValue(m_db);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CFieldValuesTable::content* pContent = (CFieldValuesTable::content*)(*pResult)[i];
		aMapValues.insert(std::make_pair(pContent->s_strName, pContent->s_strValue));
	}
	pResult->Free();
	CUserFieldsTable userField;
	userField.m_content.s_table = tp;
	pResult = userField.GetValue(m_db);
	int nUserFields = (int)pResult->GetSizeResult();
	if(nUserFields)
		((CEditorDrawer*)m_pDrawer)->AddItem(new CSectionItem(ILanguageManager::GetLanguageManager()->GetString(USER_COLUMN_ID), m_pDrawer, nBeginId++));
	for(int i = 0; i < nUserFields; i++){
		CUserFieldsTable::content* pContent = (CUserFieldsTable::content*)(*pResult)[i];
		std::tstring strValue = aCurValues[nBeginId + i];
		if(strValue.empty())
			strValue = aMapValues[pContent->s_strName];
		((CEditorDrawer*)m_pDrawer)->AddItem(new CEditItem(pContent->s_strName, nBeginId + i, GetTypeEdit(pContent->s_type), strValue));
	}
	pResult->Free();
	return nUserFields;
}

editItem::typeEdit CViewFieldValuesHelper::GetTypeEdit(TypeField::type nType){
	if(nType == TypeField::INTEGER)
		return editItem::INTEGER;
	else if(nType == TypeField::DOUBLE)
		return editItem::DOUBLE;
	else if(nType == TypeField::BOOL)
		return editItem::BOOL;
	else if(nType == TypeField::DATE)
		return editItem::DATE;
	else
		return editItem::TEXT;
}